package com.etong.citymap.activity;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.etong.citymap.adapter.ArrayWheelAdapter;
import com.etong.citymap.bean.AssignmentBean;
import com.etong.citymap.connect.HttpSendData;
import com.etong.citymap.db.util.CityApplication;
import com.etong.citymap.util.StringUtil;
import com.etong.citymap.util.log.LogUtil;
import com.etong.citymap.view.WheelView;

/**
 * 执法通申请延期
 */
public class LawYQActivity extends Activity implements OnClickListener {
	Button yq_back;
	Button yq_enter;
	EditText yq_name;
	EditText yq_ly;
	AssignmentBean bean;
	private WheelView yq_tian = null;
	private WheelView yq_shi = null;
	int miao = 0;
	private CityApplication cityApplication;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lawxq);
		cityApplication = (CityApplication)getApplication();
		yq_back = (Button) findViewById(R.id.yq_back);
		yq_enter = (Button) findViewById(R.id.yq_enter);
		yq_name = (EditText) findViewById(R.id.yq_name);
		yq_ly = (EditText) findViewById(R.id.yq_ly);
		yq_name.setText(cityApplication.getZFTUserName());
		yq_back.setOnClickListener(this);
		yq_enter.setOnClickListener(this);
		bean = (AssignmentBean) this.getIntent().getSerializableExtra("bean");
		yq_tian = (WheelView) findViewById(R.id.yq_hour);
		yq_shi = (WheelView) findViewById(R.id.yq_minutes);
		initiWheelView();
	}

	private void initiWheelView() {
		String[] tian = getResources().getStringArray(
				R.array.clockadd_tian_array);
		String[] hour = getResources().getStringArray(
				R.array.clockadd_hour_array);
		yq_shi.setAdapter(new ArrayWheelAdapter<String>(hour));
		yq_shi.setCyclic(true);
		yq_shi.setCurrentItem(1);
		yq_tian.setAdapter(new ArrayWheelAdapter<String>(tian));
		yq_tian.setCyclic(true);
		yq_tian.setCurrentItem(1);
	}

	@Override
	public void onClick(View v) {
		if (yq_back == v) {
			finish();
		}
		if (yq_enter == v) {
			UpLoadData loadData = new UpLoadData();
			loadData.execute();

		}

	}

	class UpLoadData extends AsyncTask<String, String, String> {
		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			if (StringUtil.IsNotNull(result)) {
				if (result.equals("-1") || result.equals("-2")) {
					Toast.makeText(LawYQActivity.this, "服务器出错",
							Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(LawYQActivity.this, "上传成功",
							Toast.LENGTH_SHORT).show();
					setResult(100);
					finish();
				}
			} else {
				Toast.makeText(LawYQActivity.this, "上传服务器出错",
						Toast.LENGTH_SHORT).show();
			}
		}

		@Override
		protected String doInBackground(String... params) {

			String yj = yq_ly.getText().toString();
			if (StringUtil.IsNotNull(yj)) {
				String url = "supervisor/ashx/czabnormal.ashx"; // 001 001
				Map<String, String> map = new HashMap<String, String>();
				map.put("Prjid", bean.getPrjid());
				map.put("Actinsid", bean.getActinsid());
				map.put("Insid", bean.getInsid());
				map.put("Wfid", bean.getWfid());
				map.put("assignid", bean.getAssignid());
				map.put("userid", cityApplication.getZFTUserId());
				map.put("Status", com.etong.citymap.util.Status.ZFTYQ + "");
				map.put("Idea", yj);
				miao = ((((yq_tian.getCurrentItem()-1) * 24) + (yq_shi
						.getCurrentItem()-1)) );
				map.put("Time", miao + "");
				map.put("Username", cityApplication.getZFTUserName());
				try {
					String result = HttpSendData.SendGet(url, map, "utf-8",cityApplication.getHTTPURL());
					return result;

				} catch (Exception e) {
					LogUtil.e(e);
				}

			}

			return null;
		}

	}
}
